﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Framework.Library.Base.Extensions;
using Framework.Library.Base.Helper;
using Framework.Library.Core.Config;
using Framework.Library.Core.Provider.Caches;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Core.Utils.Security;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Data.DAO;
using ServiceStack.Text;

namespace Framework.Library.Base.Base
{
    public class BasePage : Page
    {
        protected readonly ConfigHelper cfg = new ConfigHelper();
        public BaseTemplate TmpControl { get; set; }

        public LanguageHelper Localization
        {
            get { return LanguageHelper.Language; }
        }

        protected override void OnInit(EventArgs e)
        {            
            string isCache = HttpContext.Current.Request.QueryString["cache"];
            if (!string.IsNullOrEmpty(isCache))
            {
                if (isCache.Equals(Boolean.FalseString, StringComparison.CurrentCultureIgnoreCase))
                    Caching.Data.Invalidate();
            }
            string logout = HttpContext.Current.Request.QueryString["logout"];
            if (!string.IsNullOrEmpty(logout))
            {
                if (logout.Equals(Boolean.TrueString, StringComparison.CurrentCultureIgnoreCase))
                    HttpContext.Current.Session.RemovePricipal();
            }
            base.OnInit(e);
        }



        protected void RegisterMeta(Dictionary<string, string> infoTheme)
        {
            //Add Meta Tag
            Page.Header.Controls.Add(new LiteralControl(Environment.NewLine));
            foreach (var s in infoTheme)
            {
                var meta = new HtmlMeta {Name = s.Key, Content = s.Value};
                Page.Header.Controls.Add(meta);
                Page.Header.Controls.Add(new LiteralControl(Environment.NewLine));
            }
        }
        protected void RegisterCss(List<string> infoTheme)
        {
            if (infoTheme != null)
            {
                //Add Style Tag
                foreach (var s in infoTheme)
                {
                    var link = new HtmlLink();
                    link.Attributes.Add("href", Page.ResolveUrl(s));
                    link.Attributes.Add("rel", "stylesheet");
                    link.Attributes.Add("type", "text/css");
                    this.Page.Header.Controls.Add(link);
                    Page.Header.Controls.Add(new LiteralControl(Environment.NewLine));
                }
            }
        }

        protected void RegisterJavascript(List<string> infoTheme)
        {
            if (infoTheme != null)
            {
                //Add Srcipt Tag
                foreach (var s in infoTheme)
                {
                    var javascriptControl = new HtmlGenericControl("script");
                    javascriptControl.Attributes.Add("type", "text/javascript");
                    javascriptControl.Attributes.Add("src", Page.ResolveUrl(s));
                    Page.Header.Controls.Add(javascriptControl);
                    Page.Header.Controls.Add(new LiteralControl(Environment.NewLine));
                }
            }
        }

        protected void RegisterExeJavascript()
        {
            //Add Srcipt Tag
            string strJavascript = "\r\n\tvar AppEnv = { debug: " + Configurations.DEBUG_MODE.ToString().ToLower() +
                      ", cache: " + Configurations.Cache.ToString().ToLower() + ", datetime: new Date(\"" +
                      (DateTime.Now).ToString("dd MMMM yyyy HH:mm:ss") + "\") };\r\n";
            var exeJavascript = new HtmlGenericControl("script");
            exeJavascript.Attributes.Add("type", "text/javascript");
            exeJavascript.InnerHtml = strJavascript;
            this.Page.Header.Controls.Add(exeJavascript);
            Page.Header.Controls.Add(new LiteralControl(Environment.NewLine));
        }

        protected UserControl LoadControl(string UserControlPath, params object[] constructorParameters)
        {
            var ctl = Page.LoadControl(UserControlPath) as UserControl;
            if (ctl != null)
            {
                if (constructorParameters.Length > 0)
                {
                    //if (string.IsNullOrEmpty(constructorParameters[2].ToString()))
                    //    constructorParameters[2] = string.Empty;
                    var constParamTypes = new List<Type>();
                    foreach (object constParam in constructorParameters)
                    {
                        constParamTypes.Add(constParam.GetType());
                    }

                    // Tìm tất cả biến khởi tạo
                    ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray());

                    //Nếu không tìm thấy thì trả về Control
                    if (constructor != null)
                        constructor.Invoke(ctl, constructorParameters);
                }
                // Trả về Control được khởi tạo giá trị
                return ctl;
            }
            return null;
        }
    }
}